package com.ptswitch.material.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
public class Material implements Serializable,Comparable<Material> {

	private static final long serialVersionUID = 5156788179539859892L;
	@Id
	@GeneratedValue(generator = "hibernateuuid")
	@GenericGenerator(name = "hibernateuuid", strategy = "uuid")
	@Column(length = 32)
	private String id;

	@OneToMany(mappedBy = "material", fetch = FetchType.LAZY)
	@JoinColumn(name ="code",referencedColumnName="code" )
	private List<BomMaterial> bomMaterials;

	@ManyToOne(fetch = FetchType.EAGER)
	@NotFound(action = NotFoundAction.IGNORE)
//	@Cascade(value={CascadeType.DELETE,CascadeType.PERSIST,CascadeType.SAVE_UPDATE})
	private Upload upload;
	
	@OneToMany(targetEntity=Upload.class,fetch=FetchType.LAZY)
	@NotFound(action = NotFoundAction.IGNORE)
	@JoinColumn(name ="ownerId",referencedColumnName="id" )
	private List<Upload> uploads;

	@Column(columnDefinition="varchar(32) default ''")
	private String code;//物料编码
	
	@Column(columnDefinition="varchar(2048) default ''")
	private String name;//物料名称
	
	@Column(columnDefinition="varchar(2048) default ''")
	private String specification;//规格型号
	
	@Column(columnDefinition="varchar(2048) default ''")
	private String footprint;//器件封装
	
	@Column(columnDefinition="varchar(2048) default ''")
	private String brand;//品牌
	
	@Column(columnDefinition="varchar(2048) default ''")
	private String description;//器件描述
	
	@Column(columnDefinition="varchar(2048) default ''")
	private String supplier;//供货商
	
	@Column(columnDefinition="varchar(2048) default ''")
	private String pcbDecal;//pcb封装
	
	@Column(columnDefinition="varchar(32) default ''")
	private String tmpCode;
	
	@Column(columnDefinition="varchar(2048) default ''")
	private String remark;//备注
	
	@Column(columnDefinition="varchar(2048) default ''")
	private String state;//采购状态
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<BomMaterial> getBomMaterials() {
		return bomMaterials;
	}

	public void setBomMaterials(List<BomMaterial> bomMaterials) {
		this.bomMaterials = bomMaterials;
	}

	public Upload getUpload() {
		return upload;
	}

	public void setUpload(Upload upload) {
		this.upload = upload;
	}

	public List<Upload> getUploads() {
		return uploads;
	}

	public void setUploads(List<Upload> uploads) {
		this.uploads = uploads;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSpecification() {
		return specification;
	}

	public void setSpecification(String specification) {
		this.specification = specification;
	}

	public String getFootprint() {
		return footprint;
	}

	public void setFootprint(String footprint) {
		this.footprint = footprint;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getSupplier() {
		return supplier;
	}

	public void setSupplier(String supplier) {
		this.supplier = supplier;
	}

	public String getPcbDecal() {
		return pcbDecal;
	}

	public void setPcbDecal(String pcbDecal) {
		this.pcbDecal = pcbDecal;
	}

	public String getTmpCode() {
		return tmpCode;
	}

	public void setTmpCode(String tmpCode) {
		this.tmpCode = tmpCode;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String toString() {
		return this.code + "::" + this.name;
	}

	public String print() {
		StringBuilder sb = new StringBuilder();
		sb.append("code:").append(code).append(",name:").append(name).append(
				",spec:").append(specification).append(",footprint:").append(
				footprint).append(",brand:").append(brand).append(",desc:")
				.append(description).append(",supplier:").append(supplier)
				.append(",pcbDecal:").append(pcbDecal).append(",tmpCode:")
				.append(tmpCode).append(",remark:").append(remark);
		return sb.toString();
	}

	public boolean equals(Object o) {
		if (o != null && o instanceof Material) {
			return this.code.equals(((Material) o).getCode());
		}
		return false;
	}

	public int compareTo(Material o) {
		return this.code.compareTo(o.getCode());
	}
}
